//
//  EFADevInfoChartTCell.m
//  efairy
//
//  Created by XiangDongMBP on 2017/7/14.
//  Copyright © 2017年 East. All rights reserved.
//

#import "EFADevInfoChartTCell.h"
#import <Highcharts/Highcharts.h>
#import "EFAModel.h"
#import "NSDate+XDTimerFormatter.h"

@interface EFADevInfoChartTCell ()
@property (nonatomic, strong) NSArray<NSString *> *colorArray;
@end

@implementation EFADevInfoChartTCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    float screenWidth = CGRectGetWidth([UIScreen mainScreen].bounds);
    self.chartView.frame = CGRectMake(0, 0, screenWidth, screenWidth * 700 / 750);
    
    // 颜色值
    self.colorArray = @[@"7CD89E", @"FFA954", @"629CE6", @"E671D7", @"B28CE5"];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


//- (void)configView {
//    // HIOptions
//    HIOptions *options = [[HIOptions alloc] init];
//    // HIChart
//    HIChart *chart = [[HIChart alloc] init];
//    chart.type = @"line";
//    options.chart = chart;
//    // HITitle
//    HITitle *title = [[HITitle alloc] init];
//    title.text = @"Demo";
//    options.title = title;
//    // HIColumn
////    HIColumn *series = [[HIColumn alloc] init];
//    HILine *series = [[HILine alloc] init];
//    series.data = @[@49.9, @71.5, @106.4, @129.2, @144, @176, @135.6, @148.5, @216.4, @194.1, @95.6, @54.4];
//    HILine *series2 = [[HILine alloc] init];
//    series2.data = @[@39.9, @61.5, @16.4, @9.2, @149, @126, @105.6, @188.5, @116.4, @114.1, @75.6, @50.4];
//    options.series = @[series, series2];
//    // 隐藏Logo
//    HICredits *credits = [[HICredits alloc] init];
//    credits.enabled = @(NO);
//    options.credits = credits;
//    // HIGChartView
//    self.chartView.options = options;
//    [self.chartView loadChart];
//}

- (void)configViewWithEFADeviceInfoModel:(EFADeviceInfoModel *)model indexPath:(NSIndexPath *)indexPath {
    if (indexPath.row >= model.streamPtCidArray.count) {
        return;
    }
    EFADevPtCidItem *ptCid = model.streamPtCidArray[indexPath.row];
    NSInteger curPt = ptCid.pt;
    NSDictionary *ptInfoDic = [[EFADeviceProtocolModel sharedInstance] protocolInfoWithPt:curPt];
    
    // HIOptions
    HIOptions *options = [[HIOptions alloc] init];
    // HIChart
    HIChart *chart = [[HIChart alloc] init];
    chart.type = @"spline";
    chart.panning = @(YES);
//    chart.pinchType = @"xy";
    chart.zoomType = @"x";
    options.chart = chart;
//    // HITooltip(配合手势)
//    HITooltip *toolTip = [[HITooltip alloc] init];
//    toolTip.followTouchMove = @(NO);
//    options.tooltip = toolTip;
    // HITitle
    HITitle *title = [[HITitle alloc] init];
    title.text = [NSString stringWithFormat:@"%@", ptInfoDic[EFADeviceProtocolPtTitle]];
    title.align = @"left";
    title.style = ({
        HIStyle *style = [[HIStyle alloc] init];
        style.color = @"#222222";
        style.fontSize = @"15px";
        style;
    });
    options.title = title;
    // HISubTitle
    options.subtitle = ({
        HISubtitle *subTitle = [[HISubtitle alloc] init];
        subTitle.text = @"(数据发送频率1分钟/次)";
        subTitle.align = @"left";
        subTitle.style = @{@"color" : @"#888888", @"fontSize" : @"14px"};
        subTitle;
    });
    // HIExporting
    options.exporting = ({
        HIExporting *exporting = [[HIExporting alloc] init];
        exporting.enabled = @(NO);
        exporting;
    });
    // HIXAxis
    HIXAxis *xAxis = [[HIXAxis alloc] init];
    xAxis.categories = model.timestampArray;
    options.xAxis = @[xAxis];
    // HIYAxis, HIYAxisTitle
    HIYAxis *yAxis = [[HIYAxis alloc] init];
//    HIYAxisTitle * yAxisTitle = [[HIYAxisTitle alloc] init];
//    yAxisTitle.text = [NSString stringWithFormat:@"%@(%@)", ptInfoDic[EFADeviceProtocolPtTitle], ptInfoDic[EFADeviceProtocolPtUnit]];;
//    yAxis.title =  yAxisTitle;
    yAxis.title = ({
        HITitle *title = [[HITitle alloc] init];
        title.text = [NSString stringWithFormat:@"%@(%@)", ptInfoDic[EFADeviceProtocolPtTitle], ptInfoDic[EFADeviceProtocolPtUnit]];
        title;
    });
    options.yAxis = @[yAxis];
    // HILine(直线) HISpline(曲线)
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:ptCid.cidArray.count];
    [ptCid.cidArray enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        HISpline *series = [[HISpline alloc] init];
        series.name = [NSString stringWithFormat:@"%@通道%ld", [ptCid cidTitleWithCid:obj], (long)[ptCid cidDisplayWithCid:obj]];
        series.data = [model rtvArrayWithCid:obj.integerValue pt:ptCid.pt];
        NSInteger colorIndex = idx % self.colorArray.count;
        series.color = [[HIColor alloc] initWithHexValue:self.colorArray[colorIndex]];
        [mArray addObject:series];
    }];
    options.series = mArray;
    // 隐藏Logo
    HICredits *credits = [[HICredits alloc] init];
    credits.enabled = @(NO);
    options.credits = credits;
    // HIGChartView
    self.chartView.options = options;
//    [self.chartView loadChart];
}

@end


@implementation EFADevInfoChartTCell (ReportRelative)

- (void)configViewWithType:(NSInteger)type {
    // HIOptions
    HIOptions *options = [[HIOptions alloc] init];
    // HIChart
    HIChart *chart = [[HIChart alloc] init];
    chart.type = (type == 1) ? @"column" : @"pie";
    options.chart = chart;
    // HITitle
    HITitle *title = [[HITitle alloc] init];
    title.text = (type == 1) ? @"报警柱状图" : @"报警饼形图";
    title.align = @"left";
//    title.style = @{@"color" : @"#222222", @"fontSize" : @"15px"};
    title.style = ({
        HIStyle *style = [[HIStyle alloc] init];
        style.color = @"#222222";
        style.fontSize = @"15px";
        style;
    });
    options.title = title;
    // HIColumn
    if (type == 1) {
        HIColumn *series = [[HIColumn alloc] init];
        series.data = @[@(0.24), @(0.62), @(0.14)];
        series.name = @"报警";
        options.series = @[series];
    } else {
        HIPie *series = [[HIPie alloc] init];
        series.data = @[@[@"温度", @(0.24)], @[@"漏电", @(0.62)], @[@"电流", @(0.14)]];
        series.name = @"报警";
        options.series = @[series];
    }
    // HIXAxis
    HIXAxis *xAxis = [[HIXAxis alloc] init];
    xAxis.categories = @[@"温度", @"漏电", @"电流"];
    options.xAxis = @[xAxis];
//    HILine *series = [[HILine alloc] init];
//    series.data = @[@49.9, @71.5, @106.4, @129.2, @144, @176, @135.6, @148.5, @216.4, @194.1, @95.6, @54.4];
//    HILine *series2 = [[HILine alloc] init];
//    series2.data = @[@39.9, @61.5, @16.4, @9.2, @149, @126, @105.6, @188.5, @116.4, @114.1, @75.6, @50.4];
//    options.series = @[series, series2];
    // HIExporting
    HIExporting *exporting = [[HIExporting alloc] init];
    exporting.enabled = @(NO);
    options.exporting = exporting;
    // 隐藏Logo
    HICredits *credits = [[HICredits alloc] init];
    credits.enabled = @(NO);
    options.credits = credits;
    // HIGChartView
    self.chartView.options = options;
//    [self.chartView loadChart];
}

- (void)configViewWithRptAlarmScaleItem:(EFARptAlarmScaleItem *)item {
    // HIOptions
    HIOptions *options = [[HIOptions alloc] init];
    // HIChart
    HIChart *chart = [[HIChart alloc] init];
    chart.type = @"pie";
    options.chart = chart;
    // HITitle
    HITitle *title = [[HITitle alloc] init];
    title.text = [NSString stringWithFormat:@"%@报警饼形图", item.deviceTypeDes];
    title.align = @"left";
    title.style = ({
        HIStyle *style = [[HIStyle alloc] init];
        style.color = @"#222222";
        style.fontSize = @"15px";
        style;
    });
    options.title = title;
    // HIPie
    HIPie *series = [[HIPie alloc] init];
    series.data = item.pieData;
    series.name = @"报警";
    options.series = @[series];
//    // HIXAxis
//    HIXAxis *xAxis = [[HIXAxis alloc] init];
//    xAxis.categories = @[@"温度", @"漏电", @"电流"];
//    options.xAxis = @[xAxis];
    // HIExporting
    HIExporting *exporting = [[HIExporting alloc] init];
    exporting.enabled = @(NO);
    options.exporting = exporting;
    // 隐藏Logo
    HICredits *credits = [[HICredits alloc] init];
    credits.enabled = @(NO);
    options.credits = credits;
    // HIGChartView
    self.chartView.options = options;
//    [self.chartView loadChart];
}

- (void)configViewWithRptModel:(EFARptModel *)model rptType:(NSInteger)type {
    // 逻辑数据
    NSArray *array = [model sortAlarmDataWithType:type];
    NSMutableArray *yValues = [NSMutableArray arrayWithCapacity:array.count];
    NSMutableArray *xValues = [NSMutableArray arrayWithCapacity:array.count];
    NSArray *formatArray = @[@"HH点", @"MM-dd", @"MM-dd", @"MM月", @"MM月"];
    for (NSDictionary *dic in array) {
        [yValues addObject:dic[@"number"]];
        NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"dd";
        if (type < formatArray.count) {
            formatter.dateFormat = formatArray[type];
        }
        [xValues addObject:[formatter stringFromDate:dic[@"date"]]];
    }
    
    // HIOptions
    HIOptions *options = [[HIOptions alloc] init];
    // HIChart
    options.chart = ({
        HIChart *chart = [[HIChart alloc] init];
        chart.type = @"column";
        chart;
    });
    // HITitle
    HITitle *title = [[HITitle alloc] init];
    title.text = @"报警柱状图";
    title.align = @"left";
//    title.style = @{@"color" : @"#222222", @"fontSize" : @"15px"};
    title.style = ({
        HIStyle *style = [[HIStyle alloc] init];
        style.color = @"#222222";
        style.fontSize = @"15px";
        style;
    });
    options.title = title;
    // HIColumn
    HIColumn *series = [[HIColumn alloc] init];
    series.data = yValues;
    series.name = @"报警";
    series.color = [[HIColor alloc] initWithHexValue:self.colorArray.firstObject];
    options.series = @[series];
    // HIXAxis
    HIXAxis *xAxis = [[HIXAxis alloc] init];
    xAxis.categories = xValues;
    options.xAxis = @[xAxis];
    //    HILine *series = [[HILine alloc] init];
    //    series.data = @[@49.9, @71.5, @106.4, @129.2, @144, @176, @135.6, @148.5, @216.4, @194.1, @95.6, @54.4];
    //    HILine *series2 = [[HILine alloc] init];
    //    series2.data = @[@39.9, @61.5, @16.4, @9.2, @149, @126, @105.6, @188.5, @116.4, @114.1, @75.6, @50.4];
    //    options.series = @[series, series2];
    // HIYAxis, HIYAxisTitle
    HIYAxis *yAxis = [[HIYAxis alloc] init];
    yAxis.title = ({
        HITitle *title = [[HITitle alloc] init];
        title.text = @"次数";
        title;
    });
    options.yAxis = @[yAxis];
    // HIExporting
    options.exporting = ({
        HIExporting *exporting = [[HIExporting alloc] init];
        exporting.enabled = @(NO);
        exporting;
    });
    // 隐藏Logo
    options.credits = ({
        HICredits *credits = [[HICredits alloc] init];
        credits.enabled = @(NO);
        credits;
    });
    // HIGChartView
    self.chartView.options = options;
//    [self.chartView loadChart];
}

@end

